{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "from chanlun.backtesting import backtest\n",
    "from chanlun.strategy import strategy_a_3mmd\n",
    "from chanlun.cl_utils import query_cl_chart_config"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "## 注意事项\n",
    "回测前要确保已经同步回测时间段内的行情数据到数据库中\n",
    "\n",
    "同步行情数据请参考 /script/crontab/ 目录下的脚本"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "## 注意事项\n",
    "回测前要确保已经同步回测时间段内的行情数据到数据库中\n",
    "\n",
    "同步行情数据请参考 /script/crontab/ 目录下的脚本"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "# 量化配置\n",
    "bt_config = {\n",
    "    # 策略结果保存的文件\n",
    "    'save_file': './data/bk/currency_strategy_demo.pkl',\n",
    "    # 设置策略对象\n",
    "    'strategy': strategy_a_3mmd.StrategyA3mmd(),\n",
    "    # 回测模式：signal 信号模式，固定金额开仓； trade 交易模式，按照实际金额开仓\n",
    "    'mode': 'trade',\n",
    "    # 市场配置，currency 数字货币  a 沪深  hk  港股  futures  期货\n",
    "    'market': 'currency',\n",
    "    # 基准代码，用于获取回测的时间列表\n",
    "    'base_code': 'BCH/USDT',\n",
    "    # 回测的标的代码\n",
    "    'codes': ['BCH/USDT', 'EOS/USDT'],\n",
    "    # 回测的周期，这里设置里，在策略中才能取到对应周期的数据\n",
    "    'frequencys': ['30m', '5m'],\n",
    "    # 回测开始的时间\n",
    "    'start_datetime': '2022-02-01 00:00:00',\n",
    "    # 回测的结束时间\n",
    "    'end_datetime': '2022-04-01 00:00:00',\n",
    "    # 是否是股票，True 当日开仓不可平仓，False 当日开当日可平\n",
    "    'is_stock': False,\n",
    "    # 是否是期货，True 可做空，False 不可做空\n",
    "    'is_futures': True,\n",
    "     # mode 为 trade 生效，初始账户资金\n",
    "    'init_balance': 1000000,\n",
    "    # mode 为 trade 生效，交易手续费率\n",
    "    'fee_rate': 0.0006,\n",
    "    # mode 为 trade 生效，最大持仓数量（分仓）\n",
    "    'max_pos': 3,\n",
    "    # 缠论计算的配置，详见缠论配置说明\n",
    "    'cl_config': query_cl_chart_config('currency', 'BTC/USDT'),\n",
    "}\n",
    "\n",
    "BT = backtest.BackTest(bt_config)\n",
    "BT.datas.load_data_to_cache = True # 是否将所有k线数据加载到内存中，如果内存吃紧，可以设置为 False，这样每次循环都会从数据库中读取\n",
    "BT.datas.load_kline_nums = 10000 # 每次 klines 方法中返回的k线数据条数\n",
    "BT.datas.cl_data_kline_max_nums = 30000 # 回测过程中，如果缠论对象的数据量超过这个值，则会清空重新计算，cl_data_kline_max_nums 不能小于 load_kline_nums，否则将会不断的删除并重新计算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    },
    "scrolled": true,
    "tags": []
   },
   "outputs": [],
   "source": [
    "# 运行回测\n",
    "BT.run()\n",
    "# 多进程回测，只支持信号回测模式\n",
    "# BT.run_process(max_workers=4)\n",
    "# 保存回测结果到文件中\n",
    "BT.save()\n",
    "print('Done')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "# 输出回测结果\n",
    "BT.info()\n",
    "BT.result()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "# 回测数据展示\n",
    "BT.backtest_charts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "# 显示历史持仓\n",
    "#设置显示全部行，不省略\n",
    "pd.set_option('display.max_rows',None)\n",
    "#设置显示全部列，不省略\n",
    "pd.set_option('display.max_columns',None)\n",
    "\n",
    "# 显示历史持仓\n",
    "pos_df = BT.positions()\n",
    "pos_df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "# 展示图表\n",
    "BT.show_charts(BT.codes[0], BT.frequencys[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.9"
  },
  "toc-autonumbering": true
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
